package net.sourceforge.peertwitter.scamp.messaging;

import java.io.Serializable;

import rice.p2p.commonapi.Id;
import rice.p2p.commonapi.Message;
import rice.p2p.commonapi.NodeHandle;

public abstract class ScampMessage implements Serializable, Message {
	
	private final Id group;
	private final NodeHandle sender;
	private final long timestamp;
	
	public ScampMessage(Id group, NodeHandle sender, long timestamp) {
		this.group = group;
		this.sender = sender;
		this.timestamp = timestamp;
	}
	
	public ScampMessage(Id group, NodeHandle sender) {
		this(group, sender, 0);
	}

	private static final long serialVersionUID = 2379354806678335656L;

	
	public NodeHandle getSender() {
		return this.sender;
	}


	public Id getGroup() {
		return this.group;
	}

	public boolean isTimestamped() {
		return this.timestamp != 0;
	}
	
	public long getTimestamp() {
		return this.timestamp;
	}
	
//	@Override
//	public void serialize(OutputBuffer buf) throws IOException {
//		getSender().serialize(buf);
//		getGroup().serialize(buf);
//		buf.writeLong(timestamp);
//	}
	@Override
	public int getPriority() {
		return Message.MEDIUM_HIGH_PRIORITY;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((group == null) ? 0 : group.hashCode());
		result = prime * result + ((sender == null) ? 0 : sender.hashCode());
		result = prime * result + (int) (timestamp ^ (timestamp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ScampMessage other = (ScampMessage) obj;
		if (group == null) {
			if (other.group != null)
				return false;
		} else if (!group.equals(other.group))
			return false;
		if (sender == null) {
			if (other.sender != null)
				return false;
		} else if (!sender.equals(other.sender))
			return false;
		if (timestamp != other.timestamp)
			return false;
		return true;
	}

	
//	public short getType() {
//		return 0;
//	}

}
